{{ $link := .Destination }}
{{ $isRemote := strings.HasPrefix $link "http" }}
{{- if not $isRemote -}}
  <!-- BEGIN Workaround to render the links of the symlinked files  -->
  {{- if in .Page.RelPermalink "/docs/latest/project/" }}
    <!-- Replace the links pointing to the project files with the proper link -->
    {{- if in .Destination "/docs/" }}
      {{ $link = printf "../../../../%s" (replace (replace (replace (replace .Destination "docs/" "docs/latest/") ".md" "") "/README" "") "_" "-" | lower ) }}
    {{- end }}
    <!-- Replace the chart/kubeapps/readme links with the proper link -->
    {{- if in .Destination "/chart/kubeapps/" }}
      {{ $link = printf "../%s" (replace (replace (replace .Destination "/chart/kubeapps/" "/chart-readme/") ".md" "") "/README" "" | lower ) }}
    {{- end }}
    <!-- Replace "_" with "-"" -->
    <!-- If we start having issues with it, just apply the substitution if destination is "CODE_OF_CONDUCT" -->
    {{- if in .Destination "_" }}
      {{ $link = printf "../%s" (replace (replace $link "_" "-") ".md" "" | lower ) }}
    {{- end }}
  {{- end }}
  <!-- END  -->
  {{ $url := urls.Parse .Destination }}
  {{- if $url.Path -}}
    {{ $fragment := "" }}
    {{- with $url.Fragment }}
      {{ $fragment = printf "#%s" . }}
    {{ end -}}
    {{- with .Page.GetPage $url.Path }}
      {{ $link = printf "%s%s" .RelPermalink $fragment }}
    {{ end }}
  {{ end -}}
{{- end -}}

<!-- Add a title for a11y purposes  -->
{{ $title := "" }}
{{- if .Title }}
  <!-- Add the title if any  -->
  {{ $title = .Title }}
{{- else }}
  <!-- If the text description contains <, use the link's host instead -->
  {{- if in .Text ">" }}
    {{ $title = (urls.Parse .Destination).Host }}
  {{- else }}
    <!-- If not, use the link's text -->
    {{ $title = .Text }}
  {{- end }}
{{- end }}
{{- if $isRemote }}
 {{ $title = printf "%s. This link will open in a new tab" $title }}
{{- end }}
<!-- Skip rendering the badges -->
{{- if not (or (in $link "netlify.com") (in $link "circleci") (in $link "/actions/workflows/"))}}
<a href="{{ $link | safeURL }}" title="{{ $title }}" {{ if $isRemote }} target="_blank" rel="noopener" {{ end }}>{{ .Text | safeHTML }}</a>
{{- end }}
